package com.progroup.tabAndroid4;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.progroup.checks.Checks;
import com.progroup.graphics.ChartMini;
import com.progroup.kyrsmoney.R;
import com.progroup.kyrsmoney.preferences.PreferencesActivity;


public class CustomView extends CustomFragment {
	private Context context;
	private String USD_EUR_RUB = "";
	private boolean Action = false, YES =true, NO=false;
	public CustomView(Context context, String USD_EUR_RUB){
		this.context = context;
		this.USD_EUR_RUB = USD_EUR_RUB;
	}
	public LinearLayout newView(int position, final String name, final String buy,final String buy_yesterday, final String sale,final String sale_yesterday,final String image){
		LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		LinearLayout lin = (LinearLayout)li.inflate(R.layout.custom_view_layout, null);
		
		
		LinearLayout linear_1 = (LinearLayout)lin.findViewById(R.id.custom_view_layout);
		linear_1.setBackgroundResource(R.drawable.anim_click_element);
		linear_1.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				Intent  in = new Intent(context, ChartMini.class);
				in.putExtra("name", name);
				in.putExtra("kypyra", USD_EUR_RUB);
				context.startActivity(in);
			}
		});
		
		
		LinearLayout linear = (LinearLayout)linear_1.findViewById(R.id.list_linearlayout_info_bank);
		
		LinearLayout linear_buy = (LinearLayout)lin.findViewById(R.id.list_linearlayout_info_buy);
		LinearLayout linear_sale = (LinearLayout)lin.findViewById(R.id.list_linearlayout_info_sale);
		
		
		linear.setLayoutParams(new LinearLayout.LayoutParams(Checks.dpw(160), Checks.dpw(70)));
		linear_buy.setLayoutParams(new LinearLayout.LayoutParams(Checks.dpw(75), Checks.dpw(70)));
		linear_sale.setLayoutParams(new LinearLayout.LayoutParams(Checks.dpw(75),Checks.dpw(70)));
		
		
		
		
		
		
		
		TextView text_name=(TextView)linear.findViewById(R.id.list_textview_name_bank);
		final ImageView image_logo_bank = (ImageView)linear.findViewById(R.id.list_imageview_logo_bank);
		
		final TextView text_buy=(TextView)linear_buy.findViewById(R.id.list_textview_buy);
		final TextView text_buy_raznica=(TextView)linear_buy.findViewById(R.id.list_textview_buy_raznica);
		final ImageView image_buy = (ImageView)linear_buy.findViewById(R.id.list_imageview_buy_image);
		
		final TextView text_sale=(TextView)linear_sale.findViewById(R.id.list_textview_sale);
		final TextView text_sale_raznica=(TextView)linear_sale.findViewById(R.id.list_textview_sale_raznica);
		final ImageView image_sale = (ImageView)linear_sale.findViewById(R.id.list_imageview_sale_image);
		
		
		text_name.setText(name);
		
		
		new Handler().postDelayed(new Runnable() { 
			public void run() { 
				
				
				if(buy.equalsIgnoreCase("0")){
					text_buy.setText(context.getText(R.string.kyrs_activity_not_found));
					text_sale.setText(context.getText(R.string.kyrs_activity_not_found));
				}else{
					text_buy.setText(buy);
					text_sale.setText(sale);
				}
			} 
		}, 1000); 
		
		
		
		new Handler().postDelayed(new Runnable() { 
			public void run() { 
				
				
				if(!buy.equalsIgnoreCase("0")&!buy_yesterday.equalsIgnoreCase("0")){
					String raz_buy= raznica(buy, buy_yesterday);
					String raz_sale= raznica(sale, sale_yesterday);
					
					
					text_buy_raznica.setText(raz_buy);
					text_sale_raznica.setText(raz_sale);
					
					if(!buy.equalsIgnoreCase(buy_yesterday)){
						image_buy.setImageResource(getImage(buy, buy_yesterday));
					}else{
						image_buy.setVisibility(View.INVISIBLE);
					}
					if(!sale.equalsIgnoreCase(sale_yesterday)){
						image_sale.setImageResource(getImage(sale, sale_yesterday));
					}else{
						image_sale.setVisibility(View.INVISIBLE);
					}
					
				}else{
					text_buy_raznica.setVisibility(View.INVISIBLE);
					text_sale_raznica.setVisibility(View.INVISIBLE);
					image_buy.setVisibility(View.INVISIBLE);
					image_sale.setVisibility(View.INVISIBLE);
					
				}
				
				
			} 
		}, 100); 
		
		
		
		
		
		new Handler().postDelayed(new Runnable() { 
			public void run() { 
				
				
				
				
				try{
					SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
					if(pref.getBoolean(PreferencesActivity.VISIBLE_IMAGE, true)){
						image_logo_bank.setVisibility(View.VISIBLE);
						image_logo_bank.setBackgroundResource(context.getResources().getIdentifier("drawable/"+image, null, context.getPackageName()));
					}else{
						image_logo_bank.setVisibility(View.INVISIBLE);
						image_logo_bank.setLayoutParams(new LinearLayout.LayoutParams(20, 20));
					}
				}catch(Exception e){
					Log.e("EXCEPTION IMAGE", e.getMessage());
				}
				
			} 
		}, 1000); 
		
		
		
		
		
		return lin;
		
	}
	
	
	private String raznica(String today, String yesterday){
		double f1 = Double.parseDouble(today);
		double f2 = Double.parseDouble(yesterday);
		if(f1<f2){
			//Log.i("RAZNICA1", (f2-f1)+"");
			String s = (f2-f1)+"";
			if(s.length()>4){
				return "-"+((f2-f1)+"").substring(0, 5);
			}else{
				return "-"+((f2-f1)+"");
			}
		}else if(f1>f2){
			//Log.i("RAZNICA2", (f1-f2)+"");
			//Log.i("RAZNICA2", f1+"!!"+f2);
			String s = (f1-f2)+"";
			if(s.length()>4){
				return "+ "+((f1-f2)+"").substring(0, 5);
			}else{
				return "+ "+((f1-f2)+"");
			}
		}else{
			//Log.i("RAZNICA3", "0");
			return "0.000";
		}
	}
	
	private int getImage(String today, String yesterday){
		float f1 = Float.parseFloat(today);
		float f2 = Float.parseFloat(yesterday);
		if(f1<f2){
			return R.drawable.padaet;
		}else if(f1>f2){
			return R.drawable.rastet;
		}else{
			return 0;
		}
	}
	
	
}
